#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QStackedWidget>
#include <QDebug>


MainWindow::MainWindow(QWidget *parent) :
    ZDialog(parent)
{
    setMinimumSize(900, 600);
    setWindowIcon(QIcon(":/img/safe"));

    //默认皮肤
    m_strSkinName = DEFAULT_SKIN;

    initUI();
    initConnection();
}

MainWindow::~MainWindow()
{
}

void MainWindow::initUI()
{
    //标题栏
    m_titleWid = new TitleWidget();
    m_contentWid = new ContentWidget();

    m_sklinWid = new SkinWidget();

    //内容栏
    QStackedWidget *statked_widget = new QStackedWidget();
    QPalette palette;
    palette.setBrush(QPalette::Window, QBrush(Qt::white));
    statked_widget->setPalette(palette);
    statked_widget->setAutoFillBackground(true);
    statked_widget->addWidget(m_contentWid);

    QVBoxLayout *center_layout = new QVBoxLayout();
    center_layout->addWidget(statked_widget);
    center_layout->setSpacing(0);
    center_layout->setContentsMargins(0, 0, 0, 0);

    //主框架
    QVBoxLayout *main_lay = new QVBoxLayout(this);
    main_lay->addWidget(m_titleWid);
    main_lay->addLayout(center_layout);
    main_lay->setSpacing(0);
    main_lay->setContentsMargins(SHADOW_WIDTH, SHADOW_WIDTH, SHADOW_WIDTH, SHADOW_WIDTH);

    m_titleWid->turnPage("0");
}

void MainWindow::initConnection()
{
    connect(m_titleWid, SIGNAL(showSkin()), this, SLOT(showSkinWidget()));
    connect(m_titleWid, SIGNAL(showMainMenu()), this, SLOT(showMainMenu()));
    connect(m_titleWid, SIGNAL(showMin()), this, SLOT(showMinimized()));
    connect(m_titleWid, SIGNAL(closeWidget()), this, SLOT(hide()));
    connect(m_titleWid, SIGNAL(turnPage(int)), this, SLOT(turnPage(int)));

    connect(m_sklinWid, SIGNAL(changeSkin(QString)), this, SLOT(changeSkin(QString)));
}

void MainWindow::paintEvent(QPaintEvent *event)
{
    ZDialog::paintEvent(event);
    QPainter painter(this);
    painter.setPen(Qt::NoPen);
    painter.setBrush(Qt::white);
    painter.drawPixmap(QRect(SHADOW_WIDTH, SHADOW_WIDTH,
                       this->width()-2*SHADOW_WIDTH, this->height()-2*SHADOW_WIDTH),
                       QPixmap(m_strSkinName));
}

void MainWindow::showSkinWidget()
{
    qInfo() << "showSkinWidget";
    m_sklinWid->show();
}

void MainWindow::changeSkin(QString skin_name)
{
    m_strSkinName = skin_name;
    update();
}
